package com.enqualcomm.kids.mvp.oscard;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.network.socket.response.OscardProductsResult;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by Yinzhiqun on 2016/8/3.
 */
public class OscardAdapter extends RecyclerView.Adapter<OscardAdapter.ViewHolder> {
    private LayoutInflater layoutInflater;
    private OscardProductsResult.Data data;
    public OscardAdapter(LayoutInflater layoutInflater) {
        this.layoutInflater = layoutInflater;
    }


    public void setData(OscardProductsResult.Data data){
        this.data =data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_oscard, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final OscardProductsResult.Product product = data.productpay.get(position);
        holder.simpleDraweeView.setImageURI(Uri.parse(product.producturl));
        if ("1".equals(product.iffree)){//免费商品
            holder.state_tv.setText("免费");
        }else if ("1".equals(product.ifbuy)){//已购商品
            holder.state_tv.setText("已购");
        }else {//其他，显示商品单价
            holder.state_tv.setText(product.money+"￥");
        }
        holder.num_tv.setText(String.format("共%s张",product.productpicnum));
        holder.desc_tv.setText(product.productdec);
        holder.title_tv.setText(product.productname);
    }

    @Override
    public int getItemCount() {
        if (data!=null&&data.productpay!=null){
            return data.productpay.size();
        }
        return 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        public SimpleDraweeView simpleDraweeView;
        public TextView state_tv;
        public TextView num_tv;
        public TextView desc_tv;
        public TextView title_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            simpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.simpleDraweeView);
            state_tv = (TextView) itemView.findViewById(R.id.state_tv);
            num_tv = (TextView) itemView.findViewById(R.id.num_tv);
            desc_tv = (TextView) itemView.findViewById(R.id.desc_tv);
            title_tv = (TextView) itemView.findViewById(R.id.title_tv);
        }
    }
}
